/*jshint camelcase:false,proto:true*/
/**
 * @license Copyright (c) 2013 Eugene Kuzmenko
 * MIT Licensed
 */
(function () {
  'use strict';
  var proto = Object.prototype, toString = proto.toString;
  window.Util = {};
  /**
   * Creates and returns the first html element specified in a passed html
   * string or null on failure.
   * @param {string} html
   * @returns {HTMLElement|null}
   */
  window.Util.createElement = function (html) {
    var div = document.createElement('div'), i;
    div.innerHTML = html || '';
    for (i = div.childNodes.length - 1; i >= 0; i -= 1) {
      if (window.Util.isElement(div.childNodes[i])) {
        return div.childNodes[i];
      }
    }
    return null;
  };
  /**
   * Iterates over an iterable object.
   * @param {Array|Object} o An iterable object.
   * @param {Function} c A callback to execute for each element in a collection.
   *   @param {*} item A current item.
   *   @param {Number|String} i A current collection key.
   *   @param {Array|Object} collection The original collection.
   *   @return {boolean|undefined} Return false to terminate the loop.
   */
  window.Util.each = function (o, c) {
    for (var i in o) {
      if (proto.hasOwnProperty.call(o, i) && (false === c(o[i], i, o))) {
        break;
      }
    }
  };
  /**
   * Checks if passed object is an array.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isArray = function (o) {
    return '[object Array]' === toString.call(o);
  };
  /**
   * Checks if passed object is either true or false.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isBoolean = function (o) {
    return '[object Boolean]' === toString.call(o);
  };
  /**
   * Checks if passed object is an html element.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isElement = function (o) {
    return o ? o.nodeType === 1 : false;
  };
  /**
   * Checks if passed object is empty.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isEmpty = function (o) {
    return (false === o) || window.Util.isNull(o) ||
      window.Util.isUndefined(o) || (!window.Util.isUndefined(o.length) &&
      !o.length);
  };
  /**
   * Checks if passed object is a function.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isFunction = function (o) {
    return '[object Function]' === toString.call(o);
  };
  /**
   * Checks if passed object is iterable using for..in.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isIterable = function (o) {
    return window.Util.isArray(o) || window.Util.isObject(o);
  };
  /**
   * Checks if passed object is null.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isNull = function (o) {
    return '[object Null]' === toString.call(o);
  };
  /**
   * Checks if passed object is a number.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isNumber = function (o) {
    return '[object Number]' === toString.call(o);
  };
  /**
   * Checks if passed object is numeric.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isNumeric = function (o) {
    return window.Util.isNumber(o) || (window.Util.isString(o) && o.length &&
      !isNaN(o));
  };
  /**
   * Checks if passed object is an object.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isObject = function (o) {
    return '[object Object]' === toString.call(o);
  };
  /**
   * Checks if passed object is a direct instance of type Object.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isPlainObject = function (o) {
    return window.Util.isObject(o) && (o.constructor === Object);
  };
  /**
   * Checks if passed object is a string.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isString = function (o) {
    return '[object String]' === toString.call(o);
  };
  /**
   * Checks if passed object is undefined.
   * @param {*} o An object to check.
   * @return {Boolean}
   */
  window.Util.isUndefined = function (o) {
    return '[object Undefined]' === toString.call(o);
  };
  /**
   * Retrieves the prototype of an object.
   * @param {Object} o An object.
   * @return {Object} The prototype object.
   */
  window.Util.getPrototype = function (o) {
    return o && (window.Util.isUndefined(Object.getPrototypeOf) ?
      (window.Util.isUndefined(o.__proto__) ? o.constructor.prototype :
       o.__proto__) : Object.getPrototypeOf(o));
  };
}());